package com.jama.carouselviewexample.adapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.jama.carouselviewexample.ResourceTable;
import com.jama.carouselviewexample.bean.CenterBean;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

/**
 * CenterProvider
 *
 * @since 2021-05-21
 */
public class CenterProvider extends BaseItemProvider {
    private static final int NUM_20 = 20;
    private List<CenterBean> list;
    private AbilitySlice abilitySlice;

    /**
     * CenterProvider
     *
     * @param centerBeans centerBeans
     * @param slice       abilitySlice
     */
    public CenterProvider(List<CenterBean> centerBeans, AbilitySlice slice) {
        this.list = centerBeans;
        this.abilitySlice = slice;
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * getItem
     *
     * @param position position
     * @return Object
     */
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    /**
     * getItemId
     *
     * @param position position
     * @return long
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * getComponent
     *
     * @param i                  i
     * @param component          component
     * @param componentContainer componentContainer
     * @return Component
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt = null;
        CenterProvider.CenterHolder holder = null;
        CenterBean centerBean = list.get(i);
        switch (centerBean.getPos()) {
            case 0:
                if (component == null) {
                    cpt = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_item_center_one,
                            null, false);
                    holder = new CenterProvider.CenterHolder(cpt);
                    // 将获取到的子组件信息绑定到列表项的实例中
                    cpt.setTag(holder);
                } else {
                    cpt = component;
                    // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
                    holder = (CenterProvider.CenterHolder) cpt.getTag();
                }
                break;
            case 1:
                if (component == null) {
                    cpt = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_item_center_two,
                            null, false);
                    holder = new CenterProvider.CenterHolder(cpt);
                    // 将获取到的子组件信息绑定到列表项的实例中
                    cpt.setTag(holder);
                } else {
                    cpt = component;
                    // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
                    holder = (CenterProvider.CenterHolder) cpt.getTag();
                }
                break;
            case 2:
                if (component == null) {
                    cpt = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_item_center_three,
                            null, false);
                    holder = new CenterProvider.CenterHolder(cpt);
                    // 将获取到的子组件信息绑定到列表项的实例中
                    cpt.setTag(holder);
                } else {
                    cpt = component;
                    // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
                    holder = (CenterProvider.CenterHolder) cpt.getTag();
                }
                break;
            default:
                break;
        }
        if (abilitySlice.getContext() != null) {
            Glide.with(abilitySlice.getContext())
                    .load(centerBean.getImageId())
                    .centerCrop()
                    .transform(new RoundedCorners(NUM_20))
                    .into(holder.image);
        }
        return cpt;
    }

    /**
     * CenterHolder
     *
     * @since 2021-05-21
     */
    static class CenterHolder {
        private Image image;

        CenterHolder(Component component) {
            this.image = (Image) component.findComponentById(ResourceTable.Id_iv_center_image);
        }

        public Image getImage() {
            return image;
        }

        public void setImage(Image img) {
            this.image = img;
        }
    }
}
